package com.ttloner1.connection;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author TTLoner
 * @date 2021/6/20 19:14
 * @description
 */
public class ConnectionTest {
    //方式一：
    @Test
    public void testConnection1() throws SQLException {
        //获取driver的实现类对象
        Driver driver = new com.mysql.jdbc.Driver();
        //url固定操作  url:http://loclhost:8080/gmall/keyboard.jpg
        //协议+ip+端口+资源，test是数据库名
        String url = "jdbc:mysql://localhost:3306/test";
        //将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "root");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }
    @Test
    //方法二:使用反射，不出现三方包名，具有更好的可移植性
    public void connection2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //获取driver的实现类对象,使用反射
        Class aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        //提供要连接的数据库
        String url = "jdbc:mysql://localhost:3306/test";
        //将用户名和密码封装在Properties中
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "root");
        Connection conn = driver.connect(url, info);
        System.out.println(conn);
    }
    @Test
    //方式三:使用driverManager替换Driver,比较常用
    public void testConnection() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //1.获取driver的实现类对象,使用反射
        Class aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        //2.提供三个基本信息
        String url ="jdbc:mysql://localhost:3306/test";
        String username="root";
        String password="root";
        //3.注册驱动
        DriverManager.registerDriver(driver);
        //4.获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println(connection);
    }
    @Test
    //方式四：对于方式三的优化，注册驱动上。在mysql的Driver实现类中，声明了如下注释掉的操作
    public void testConnection4() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        //1.获取driver的实现类对象,使用反射
        Class.forName("com.mysql.jdbc.Driver");
        //Driver driver = (Driver) aClass.newInstance();
        //2.提供三个基本信息
        String url ="jdbc:mysql://localhost:3306/test";
        String username="root";
        String password="root";
        //3.注册驱动
        //DriverManager.registerDriver(driver);
        //4.获取连接
        Connection connection = DriverManager.getConnection(url, username, password);
        System.out.println(connection);
    }
    @Test
    //方式五:最终版,将数据库连接需要的四个基本信息放在配置文件中
    /*
    此方法的好处：1.实现了数据于代码的分离，实现了解耦
    2.如果需要修改配置文件信息，可以避免程序重新打包
     */
    public void testConnection5() throws IOException, ClassNotFoundException, SQLException {
        //1.读取配置文件中四个基本信息
        InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");
        //2.加载驱动
        Class.forName(driverClass);
        //3.获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }
}

